﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Projektarbete
{
    class Map
    {
        public Tile[][] map;
        private Texture2D tileset;

        public Point mapDimensions;

        public Point topLeft;
        public Point bottomRight;

        public LinkedList<Merchant> merchantList;

        /// <summary>
        /// Creates the map system with the following starting map
        /// </summary>
        /// <param name="map">Starting map to use</param>
        public Map(Tile[][] map, Texture2D tileset)
        {
            this.map = map;
            this.tileset = tileset;

            topLeft = Point.Zero;
            bottomRight = Point.Zero;

            mapDimensions = new Point(map.Length, map[0].Length);
        }

        public void ChangeTile(Tile tile, Point position)
        {
            map[position.Y][position.X] = tile;
        }

        public Merchant GetMerchant(Point position)
        {
            Vector2 VectorPosition = new Vector2(position.Y * 24, position.X * 24);

            foreach (Merchant merchant in merchantList)
            {
                if (merchant.Position == VectorPosition)
                {
                    return merchant;
                }
            }

            throw new Exception("Borde inte hända. Ropa på David");
        }

        public void SetMerchantlist(LinkedList<Merchant> merchantList)
        {
            this.merchantList = merchantList;
        }

        public void changeMap(Tile[][] map)
        {
            this.map = map;
            mapDimensions = new Point(map.Length, map[0].Length);
            //TODO: Transition effect
        }

        public void drawMap(SpriteBatch spriteBatch, Vector2 tiledPosition)
        {
            for (int y = 0; y < mapDimensions.Y; y++)
            {
                for (int x = 0; x < mapDimensions.X; x++)
                {
                    spriteBatch.Draw(tileset, new Vector2(y * 24, x * 24), new Rectangle((int)map[x][y].textureID * 24, 0, 24, 24), Color.White);
                }
            }
        }
    }
}